package qqclient.service;

import qqcommon.Message;
import qqcommon.MessageType;

import java.io.*;
import java.net.Socket;

/**
 * @author DCSGO
 * @version 1.0
 * 客户端与服务端连接线程类
 * 用于监听服务端发送的消息
 */
public class ClientConnectServerThread extends Thread {
    private final Socket socket;

    @Override
    public void run() {
        while (true) {
            try {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();
                switch (message.getMesType()) {
                    case MessageType.MESSAGE_RET_ONLINE_USERS:
                        String[] users = message.getContent().split(" ");
                        System.out.println("\n====所有在线用户====");
                        for (String user : users) {
                            System.out.println("用户：" + user);
                        }
                        break;
                    case MessageType.MESSAGE_COMM_MES:
                        System.out.println("\n" + message.getSender() + " 向你发送消息( " + message.getSendTime() + " )");
                        System.out.println(message.getContent());
                        break;
                    case MessageType.MESSAGE_FAIL_IN_SEND:
                        System.out.println("\n你向 " + message.getReceiver() + " 发送的消息，发送失败！");
                        System.out.println("失败原因：" + message.getContent());
                        break;
                    case MessageType.MESSAGE_TO_ALL_MES:
                        System.out.println("\n收到来自 " + message.getSender() + " 的广播消息：");
                        System.out.println(message.getContent());
                        break;
                    case MessageType.MESSAGE_FILE_MES:
                        System.out.println("\n" + message.getSendTime());
                        System.out.println(message.getSender() + " 向你发送文件：\n" + message.getFileName());
                        System.out.println("大小：" + String.format("%.3f", message.getFileLen() * 1.0 / 1024 / 1024) + " MB");
                        File file = new File(message.getDestFilePath());
                        if (!file.exists()) {
                            File parent = file.getParentFile();
                            if (!parent.exists()) {
                                if (!parent.mkdirs()) {
                                    System.out.println("父级目录创建失败！");
                                }
                            }
                            if (!file.createNewFile()) {
                                System.out.println("文件接收失败！");
                                continue;
                            }
                        }
                        try (FileOutputStream fos = new FileOutputStream(file)) {
                            fos.write(message.getFileBytes());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.out.println("已将其保存到此电脑：\n" + message.getDestFilePath());
                        break;
                    default:
                        System.out.println("\n其他消息类型，暂未处理...");
                        break;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public Socket getSocket() {
        return socket;
    }

    public ClientConnectServerThread(Socket socket) {
        this.socket = socket;
    }
}
